package org.jeecg.modules.api.wlhy.losssetting;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.wlhy.customer.entity.HyCustomer;
import org.jeecg.modules.wlhy.customer.service.IHyCustomerService;
import org.jeecg.modules.wlhy.losssetting.entity.HyLossGoodsUnitPrice;
import org.jeecg.modules.wlhy.losssetting.service.IHyLossGoodsUnitPriceService;
import org.jeecg.modules.wlhy.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description: 亏吨价格设置接口
 * @author: luruirui
 * @date: 2021年04月14日 13:58
 */
@Api(tags="APP亏损货物单价设置")
@RestController
@RequestMapping("/api/wlhy/lossPrice")
@Slf4j
public class ApiLossPriceController extends JeecgController<HyLossGoodsUnitPrice, IHyLossGoodsUnitPriceService> {
    @Autowired
    private IHyLossGoodsUnitPriceService hyLossGoodsUnitPriceService;
    @Autowired
    private IHyCustomerService hyCustomerService;
    @Autowired
    private UserUtil userUtil;
    /**
     * 分页列表查询
     *
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @ApiOperation(value="亏损货物单价表-分页列表查询", notes="亏损货物单价表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<HyLossGoodsUnitPrice> queryWrapper = QueryGenerator.initQueryWrapper(new HyLossGoodsUnitPrice(), req.getParameterMap());

        LoginUser loginUser = UserUtil.getLoginUser();
        String userId = loginUser.getId();
        QueryWrapper<HyCustomer> customerWrapper = QueryGenerator.initQueryWrapper(new HyCustomer(), req.getParameterMap());
        customerWrapper.eq("user_id", userId);
        HyCustomer customer = hyCustomerService.getOne(customerWrapper);
        if(customer == null){
            return Result.error("货主信息不存在!");
        }

        queryWrapper.eq("customer_id",customer.getId());
        Page<HyLossGoodsUnitPrice> page = new Page<HyLossGoodsUnitPrice>(pageNo, pageSize);
        IPage<HyLossGoodsUnitPrice> pageList = hyLossGoodsUnitPriceService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     *   添加
     *
     * @param jsonObject
     * @param req
     * @return
     */
    @AutoLog(value = "亏损货物单价表-添加")
    @ApiOperation(value="亏损货物单价表-添加", notes="亏损货物单价表-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody JSONObject jsonObject, HttpServletRequest req) {
        LoginUser loginUser = UserUtil.getLoginUser();
        String userId = loginUser.getId();
        QueryWrapper<HyCustomer> wrapper = QueryGenerator.initQueryWrapper(new HyCustomer(), req.getParameterMap());
        wrapper.eq("user_id", userId);
        HyCustomer customer = hyCustomerService.getOne(wrapper);
        if(customer == null){
            return Result.error("货主信息不存在!");
        }
        Integer price = jsonObject.getInteger("price");
        String goodsName = jsonObject.getString("goodsName");



        HyLossGoodsUnitPrice hyLossGoodsUnitPrice = new HyLossGoodsUnitPrice();
        hyLossGoodsUnitPrice.setCustomerId(customer.getId());
//        hyLossGoodsUnitPrice.setCreateBy(loginUser.getUsername());
//        hyLossGoodsUnitPrice.setCreateTime(new Date());
        hyLossGoodsUnitPrice.setLossGoodsUnitPrice(price);
        hyLossGoodsUnitPrice.setGoodsName(goodsName);
        hyLossGoodsUnitPriceService.save(hyLossGoodsUnitPrice);
        return Result.OK("添加成功！");
    }

    /**
     *  编辑
     *
     * @param hyLossGoodsUnitPrice
     * @return
     */
    @AutoLog(value = "亏损货物单价表-编辑")
    @ApiOperation(value="亏损货物单价表-编辑", notes="亏损货物单价表-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody HyLossGoodsUnitPrice hyLossGoodsUnitPrice) {
        hyLossGoodsUnitPriceService.updateById(hyLossGoodsUnitPrice);
        return Result.OK("编辑成功!");
    }

    /**
     *   通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "亏损货物单价表-通过id删除")
    @ApiOperation(value="亏损货物单价表-通过id删除", notes="亏损货物单价表-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name="id",required=true) String id) {
        HyLossGoodsUnitPrice lossGoodsUnitPrice = hyLossGoodsUnitPriceService.getById(id);
        if(lossGoodsUnitPrice == null){
            return Result.error("数据不存在!");
        }
        hyLossGoodsUnitPriceService.removeById(id);
        return Result.OK("删除成功!");
    }
}
